package com.dmooo.ymx.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dmooo.ymx.R;
import com.dmooo.ymx.adapter.AccountAdapter;
import com.dmooo.ymx.adapter.MessageAdapter;
import com.dmooo.ymx.adapter.ShopMallRecyclerAdapter;
import com.dmooo.ymx.base.BaseActivity;
import com.dmooo.ymx.bean.AccPass;
import com.dmooo.ymx.bean.MessageCenterBean;
import com.dmooo.ymx.bean.Response;
import com.dmooo.ymx.bean.Wphbean;
import com.dmooo.ymx.common.ACache;
import com.dmooo.ymx.common.SPUtils;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.https.HttpUtils;
import com.dmooo.ymx.https.onOKJsonHttpResponseHandler;
import com.dmooo.ymx.login.LoginActivity;
import com.dmooo.ymx.login.WelActivity;
import com.dmooo.ymx.utils.ListDataSave;
import com.dmooo.ymx.utils.SlideRecyclerView;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

public class ChangeAccountActivity extends BaseActivity {
    @BindView(R.id.recy_focus)
    SlideRecyclerView recyFocus;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.addAccount)
    Button addAccount;
    AccountAdapter adapter;
    List<AccPass> accPassesList = new ArrayList<>();
    private int page = 1;

    List<AccPass> classTests=new ArrayList<>();

    ListDataSave dataSave;
    private ACache mAcache;
    private List<Object> listBean;


    @Override
    protected void initUI() {
        setContentView(R.layout.activity_change_acc);
        ButterKnife.bind(this);
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("切换账号");
        mAcache = ACache.get(this);
        listBean = new ArrayList<>();
        dataSave = new ListDataSave(ChangeAccountActivity.this, "baiyu");
    }

    @Override
    protected void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyFocus.setLayoutManager(linearLayoutManager);
//        recyFocus.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        recyFocus.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        initAdapter();
        adapter = new AccountAdapter(ChangeAccountActivity.this,R.layout.item_account, classTests);
        recyFocus.setAdapter(adapter);
        recyFocus.closeMenu();
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle=new Bundle();
                bundle.putString("acount",classTests.get(position).getAccount());
                bundle.putString("password",classTests.get(position).getPassword());
                openActivity(LoginActivity.class,bundle);
                SetActivity.activity.finish();
                finish();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        addAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                openActivity(WelActivity.class);
                SetActivity.activity.finish();
                finish();
            }
        });
    }


    private void initAdapter(){
        classTests=new ArrayList<>();
        try{
            for(int i=0;i<dataSave.getDataList("javaBean").size();i++){
                classTests.add(new Gson().fromJson(dataSave.getDataList("javaBean").get(i).toString(), AccPass.class));
            }
            remove(classTests);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static ArrayList list(ArrayList al)
    {
        ArrayList newal = new ArrayList();//新建一个中间集合
        for(Iterator it = al.iterator();it.hasNext();)//集合循环
        {
            Object obj = it.next();
            if(!newal.contains(obj))//不包含重复的输出
                newal.add(obj);
        }
        return newal;
    }

    //去Bean类重
    public void remove(List<AccPass> list){
        Log.e("before del depulicate",list.size()+"");
        Set<AccPass> set = new HashSet<>();
        List<AccPass> listaaa = new ArrayList<>();
        for (Iterator<AccPass> ite = list.iterator();ite.hasNext();){
            AccPass aaa = ite.next();
            if (set.add(aaa)){
                listaaa.add(aaa);
            }
        }
        list.clear();
        list.addAll(listaaa);
    }

    public static <T> List<T> getObjectList(String jsonString,Class<T> cls){
        List<T> list = new ArrayList<T>();
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    protected void initListener() {
        adapter.setOnDeleteClickListener(new AccountAdapter.OnDeleteClickLister() {
            @Override
            public void onDeleteClick(View view, int position) {
                listBean=dataSave.getDataList("javaBean");
                if(listBean.size()<=1){
                    showToast("最少保留一个账号");
                }else{
                    listBean.remove(position);
                    dataSave.setDataList("javaBean",listBean);
                    classTests.remove(position);
                    adapter.notifyDataSetChanged();
                }
                recyFocus.closeMenu();
            }
        });
    }
}
